package com.useeinfo.oa.open.api.taskOrder;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.func.lang.StringUtils;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.biz.AreaBiz;
import com.useeinfo.oa.modules.base.biz.EnterpriseBiz;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.base.entity.Position;
import com.useeinfo.oa.modules.inspection.biz.InspectionOrderBiz;
import com.useeinfo.oa.modules.maintaining.biz.MaintainOrderBiz;
import com.useeinfo.oa.modules.task.biz.TaskFocusBiz;
import com.useeinfo.oa.modules.task.biz.TaskOrderBiz;
import com.useeinfo.oa.modules.task.entity.TaskFocus;
import com.useeinfo.oa.modules.task.entity.TaskOrder;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;


@RestController
@RequestMapping("/taskDashboardReport")
public class TaskDashboardReportAPI {
    @Autowired
    private EnterpriseBiz enterpriseBiz;
    @Autowired
    private TaskOrderBiz taskOrderBiz;
    @Autowired
    private TaskFocusBiz taskFocusBiz;
    @Autowired
    private SystemUserBiz systemUserBiz;


    /**
     * 获取与我相关得任务工单
     * @param userId 用户id
     * @param pageIndexParam
     * @param pageSizeParam
     * @param enterpriseId 企业
     * @param type 类型 （关注，发起，审核，待接单）
     * @return JSONObject
     */
    @RequestMapping(value = "/getOrderList")
    public JSONObject getOrderList(@RequestParam(value = "userId", required = false) String userId,
                                   @RequestParam(value = "pageIndex", required = false) String pageIndexParam,
                                   @RequestParam(value = "pageSize", required = false) String pageSizeParam,
                                   @RequestParam(value = "enterpriseId", required = false) String enterpriseId,
                                   @RequestParam(value = "type", required = false) String type) {
        JSONObject data = new JSONObject();
        data.put("records", 0);
        JSONArray rows = new JSONArray();
        data.put("rows", rows);
        if (StringUtils.isBlank(type)) {
            return data;
        }
        SystemUser user = systemUserBiz.findModel(StringConverters.ToLong(userId));
        Enterprise enterprise = enterpriseBiz.findModel(StringConverters.ToLong(enterpriseId));
        if (user == null || enterprise == null) {
            return data;
        }
        //查关注的工单
        if ("focus".equals(type)) {
            QueryParam queryParamF = new QueryParam(pageIndexParam, pageSizeParam);
            queryParamF.getSqlMap().put("systemUserId", userId);
            queryParamF.getSqlMap().put("enterpriseId", enterpriseId);
            List<TaskFocus> taskFocusList = taskFocusBiz.findList(queryParamF);
            if (CollectionUtils.isNotEmpty(taskFocusList)) {
                for (TaskFocus taskFocus : taskFocusList) {
                    if (taskFocus.getTaskOrder() != null) {
                        TaskOrder taskOrder = taskFocus.getTaskOrder();
                        JSONObject json = taskOrder.toJSONObject();
                        JSONArray tags = taskOrderBiz.getTags(taskOrder, user, null);
                        json.put("tags", tags);
                        rows.add(json);
                    }
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("systemUserId", userId);
            queryParam.getSqlMap().put("enterpriseId", enterpriseId);
            Long records = taskFocusBiz.totalRecord(queryParam.getSqlMap());
            if (records > 0) {
                data.put("records", records);
            }
        }
        //查发起的工单
        if ("create".equals(type)) {
            QueryParam queryParamC = new QueryParam(pageIndexParam, pageSizeParam);
            queryParamC.getSqlMap().put("createId", userId);
            queryParamC.getSqlMap().put("enterpriseId", enterpriseId);
            List<TaskOrder> taskOrderList = taskOrderBiz.findList(queryParamC);
            if (CollectionUtils.isNotEmpty(taskOrderList)) {
                for (TaskOrder taskOrder : taskOrderList) {
                    JSONObject json = taskOrder.toJSONObject();
                    JSONArray tags = taskOrderBiz.getTags(taskOrder, user, null);
                    json.put("tags", tags);
                    rows.add(json);
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("createId", userId);
            queryParam.getSqlMap().put("enterpriseId", enterpriseId);
            Long records = taskOrderBiz.totalRecord(queryParam.getSqlMap());
            if (records > 0) {
                data.put("records", records);
            }
        }
        //查我接单的
        if ("receive".equals(type)) {
            QueryParam queryParamR = new QueryParam(pageIndexParam, pageSizeParam);
            queryParamR.getSqlMap().put("receiveUser", userId);
            queryParamR.getSqlMap().put("enterpriseId", enterpriseId);
            List<TaskOrder> taskOrderList = taskOrderBiz.findList(queryParamR);
            if (CollectionUtils.isNotEmpty(taskOrderList)) {
                for (TaskOrder taskOrder : taskOrderList) {
                    JSONObject json = taskOrder.toJSONObject();
                    JSONArray tags = taskOrderBiz.getTags(taskOrder, user, null);
                    json.put("tags", tags);
                    rows.add(json);
                }
            }
            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("receiveUser", userId);
            queryParam.getSqlMap().put("enterpriseId", enterpriseId);
            Long records = taskOrderBiz.totalRecord(queryParam.getSqlMap());
            if (records > 0) {
                data.put("records", records);
            }
        }
        //我审核的
        if ("audit".equals(type)) {
            //查出所属岗位
            QueryParam queryParamA = new QueryParam(pageIndexParam, pageSizeParam);
            queryParamA.getSqlMap().put("auditUserId", userId);
            queryParamA.getSqlMap().put("enterpriseId", enterpriseId);
            queryParamA.getSqlMap().put("orderStatus","IN_AUDITING");
            Position position = user.getPosition(enterprise, user.getPositionSet());
            if (position != null) {
                queryParamA.getSqlMap().put("auditPositionId", position.getId() + "");
            }
            List<TaskOrder> taskOrderList = taskOrderBiz.findList(queryParamA);
            if (CollectionUtils.isNotEmpty(taskOrderList)) {
                for (TaskOrder taskOrder : taskOrderList) {
                    JSONObject json = taskOrder.toJSONObject();
                    JSONArray tags = taskOrderBiz.getTags(taskOrder, user, null);
                    json.put("tags", tags);
                    rows.add(json);
                }
            }

            //查总数
            QueryParam queryParam = new QueryParam();
            queryParam.getSqlMap().put("auditUserId", userId);
            queryParam.getSqlMap().put("enterpriseId", enterpriseId);
            queryParam.getSqlMap().put("orderStatus","IN_AUDITING");
            if (position != null) {
                queryParam.getSqlMap().put("auditPositionId", position.getId() + "");
            }
            Long records = taskOrderBiz.totalRecord(queryParam.getSqlMap());
            if (records > 0) {
                data.put("records", records);
            }
        }
        //待接单的
        if ("waitTaking".equals(type)) {
            ResultDto resultDto = taskOrderBiz.getTaskOrders(pageIndexParam, pageSizeParam, "1", null, user, enterprise, null);
            JSONObject jsonObject = JSONObject.parseObject(resultDto.getData().toString());
            rows = jsonObject.getJSONArray("rows");
            ResultDto resultDto1 = taskOrderBiz.getTaskOrders("-1", "-1", "1", null, user, enterprise, null);
            JSONObject jsonObject1 = JSONObject.parseObject(resultDto1.getData().toString());
            JSONArray array = jsonObject1.getJSONArray("rows");
            if (array.size() > 0) {
                data.put("records", array.size());
            }
        }
        data.put("rows", rows);
        return data;
    }
}
